<template>
  <section>

    <el-drawer
        title="当前数据字典类型明细"
        :visible.sync="table"
        direction="ttb"
        size="60%">

      <!--数据字典-明细-列表-->
      <el-table :data="detailData" highlight-current-row v-loading="listLoading" style="width: 100%;">
        <el-table-column type="index" width="60">
        </el-table-column>

        <el-table-column prop="name" label="字典明细名称" width="150" sortable>
        </el-table-column>

      </el-table>
      
    </el-drawer>

    <!--数据字典-类型-高级查询-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="请输入查询内容"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getTypes">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--数据字典-类型-列表展示-->
    <el-table @row-dblclick="showDetail" :data="types" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="字典名称" width="160" sortable>
      </el-table-column>
      <el-table-column prop="sn" label="字典编号" width="160" sortable>
      </el-table-column>

      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--数据字典-类型-批量删除-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :page-size="pageSize"
          :total="total"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--数据字典-类型-编辑界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="字典编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>

export default {
  data() {
    return {
      table: false,
      detailData: [],
      title: "",
      filters: {
        keyword: ''
      },
      filters1: {
        keyword: ''
      },
      types: [],
      total: 0,
      total1: 0,
      pageSize: 5,
      page: 1,
      page1:1,
      listLoading: false,
      sels: [],//列表选中列
      sels1: [],
      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [
          { required: true, message: '请输入字典名称', trigger: 'blur' } // blur:失去焦点
        ],
        sn: [
          { required: true, message: '请输入字典编号', trigger: 'blur' } // blur:失去焦点
        ]
      },
      //编辑界面数据
      editForm: {
        id: null,
        sn: "",
        name: "",
      },
      editForm1: {
        id: null,
        name: "",
        type: null,  // 传递的应该是对象
      }

    }
  },
  methods: {
    
    showDetail(row){
      console.debug(row);
      let para = {};
        if(row.id){
          para = {
            currentPage: this.page1,
            pageSize: this.pageSize,
            keyword: this.filters1.keyword,
            typeId: row.id
          };
        }else{
          para = {
            currentPage: this.page1,
            pageSize: this.pageSize,
            keyword: this.filters1.keyword,
            typeId: this.detailData[0].type.id
          };
        }
        
      this.$http.post("/detail",para)
          .then(result=>{
            this.detailData = result.data.rows;
            this.total1 = result.data.total;
            this.table =true;
          })
    },
    
    handleCurrentChange(val) {
      this.page = val;
      this.getTypes();
    },

    //获取字典类型列表
    getTypes() {
      let para = {
        currentPage: this.page,
        pageSize: this.pageSize,
        keyword: this.filters.keyword
      };
      this.listLoading = true; // 忙等框
      // 发送请求
      this.$http.post("/type",para)
          .then(result=>{
            this.listLoading = false; // 忙等框
            this.types = result.data.rows;
            this.total = result.data.total;
          }).catch(result=>{
        this.listLoading = false; // 忙等框
        console.debug(result)
      })
    },
    
    //删除
    handleDel: function (index, row) {
      this.$confirm('删除数据字典类型会同时删除数据字典明细,您确定要删除吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;  // 忙等框
        //
        this.$http.delete("/type/"+row.id)
            .then(result=>{ // ajaxResult  success  msg
              this.listLoading = false;  // 忙等框
              if(result.data.success){

                this.$message({
                  message: '删除成功',
                  type: 'success'
                });

              }else{
                this.$message({
                  message: result.data.msg,
                  type: 'error'
                });
              }
              this.page = 1;
              this.getTypes();
            }).catch(result=>{
          this.$message({
            message: "系统异常",
            type: 'error'
          });
        })

      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.title="编辑"
      this.editFormVisible = true; // 弹出编辑界面

      this.editForm = Object.assign({}, row);  // 回显
    },
    //显示新增界面
    handleAdd: function () {
      this.title="新增"
      this.editFormVisible = true;
      this.editForm = {
        id: null,
        sn: "",
        name: "",
      };
    },
    //提交
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交数据吗？', '温馨提示', {}).then(() => {
            this.editLoading = true;  // 忙等框
            //NProgress.start();
            let para = Object.assign({}, this.editForm); // 克隆

            this.$http.put("/type",para)
                .then(result=>{
                  if(result.data.success){
                    this.$message({
                      message: '提交成功',
                      type: 'success'
                    });
                  }else{
                    this.$message({
                      message: result.data.msg,
                      type: 'error'
                    });
                  }

                  this.editLoading = false;  // 忙等框
                  this.editFormVisible = false;

                  this.getTypes();
                }).catch(result=>{
              this.editLoading = false;  // 忙等框
              this.editFormVisible = false;
              alert("系统异常，请重新操作");
            })

          })
        }
      });
    },
    
    selsChange: function (sels) {

      this.sels = sels;
    },
    
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id) // [1,2,3,4,6]   // .toString();  // 1,2,3,4,5   1
      this.$confirm('确认批量删除选中的记录吗？', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true; // 开启忙等框
        this.$http.patch("/type",ids)
            .then(result=>{
              this.listLoading = false;
              if(result.data.success){
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              }else{
                this.$message({
                  message: result.data.msg,
                  type: 'error'
                });
              }

              // 定位到第一页
              this.page = 1;
              this.getTypes();

            }).catch(result=>{
          this.$message({
            message: "系统异常，请重新操作",
            type: 'error'
          });
        })

      }).catch(() => {

      });
    },

  },
  mounted() {
    // 查询数据字典类型
    this.getTypes();

  }
}

</script>

<style scoped>

</style>